package com.zizo.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import org.apache.http.util.ByteArrayBuffer;

public class Zip {
	/**
	 * @Title: readZipFile
	 * @Description: TODO 读取Zip文件中的一个文件的内容
	 * @param file
	 *            Zip文件路径
	 * @param fileName
	 *            Zip文件中的文件
	 * @return void
	 * @author Aleixs
	 */
	public static String readZipFile(String file, String fileName) {
		StringBuffer result = new StringBuffer();
		try {
			@SuppressWarnings("resource")
			ZipFile zf = new ZipFile(file);
			ZipEntry ze = zf.getEntry(fileName);
			InputStream in = zf.getInputStream(ze);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String line;
			result = new StringBuffer();
			while ((line = br.readLine()) != null) {
				result.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result.toString();
	}

	/**
	 * @Title: getZipFileList
	 * @Description: TODO 获得zip文件中的所有文件列表，这里的文件都为File，没有目录格式的文件
	 * @param pathname
	 * @return
	 * @return List<String>
	 * @author Alexis
	 */
	public static List<String> getZipFileList(String pathname) {
		List<String> filelist = new ArrayList<>();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(pathname));
			@SuppressWarnings("resource")
			ZipInputStream zin = new ZipInputStream(in);
			ZipEntry ze;
			while ((ze = zin.getNextEntry()) != null) {
				filelist.add(ze.getName());
			}
			zin.closeEntry();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return filelist;
	}

	/**
	 * @Title: runCommand
	 * @Description: TODO 运行一个命令，将文件压缩为Zip文件
	 * @param commands
	 * @return void
	 * @author Alexis
	 */
	public static void runCommand(String commands) {
		Runtime run = Runtime.getRuntime();// 返回与当前 Java 应用程序相关的运行时对象
		try {
			run.exec(commands);// 启动另一个进程来执行命令
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("resource")
	public static String visitGZip(File targzFile) throws IOException {
		FileInputStream fileIn = null;
		BufferedInputStream bufIn = null;
		GZIPInputStream gzipIn = null;
		ByteArrayBuffer bt = new ByteArrayBuffer(4096);
		String resultString = "";
		try {
			fileIn = new FileInputStream(targzFile);
			bufIn = new BufferedInputStream(fileIn);
			gzipIn = new GZIPInputStream(bufIn); // first unzip the input file
			int l;
			byte[] tmp = new byte[1024];
			while ((l = gzipIn.read(tmp)) != -1) {
				bt.append(tmp, 0, l);
			}

			resultString = new String(bt.toByteArray(), "utf-8");
			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultString;
	}

	public static void main(String[] args) throws Exception {
		File file = new File("C:\\17770.html");
		System.out.println(visitGZip(file));
	}
}
